﻿#include <stdio.h>
#include <stdlib.h>

#include "functions.h"
#include "HTTP.h"
#include "packet.h"
#include "tropusr_parser.h"
#include "memory.h"
#include "oskdialog.h"
#include <dirent.h>

// ----- DEFINES ----- //
#define N_IMAGES 4	  

// ----- STRUCTS ----- //
enum userAction{
		ACTION_MENU = 0,
		ACTION_TROPHY_LOAD,
		ACTION_TROHPY,
}uAction = ACTION_MENU;

// ----- EXTERN ----- //
extern char *_packet;
extern char *_packet_raw;
extern char *_path;
extern char *_buffer;


// ----- VARS ----- //
CSocket *csocket = NULL;
SOCKET sock = NULL;
signed char login_process = -1;
char user[80];
char pass[80];
char c_pass[80] = "";
int running = 1;
SDL_Event event;
SDL_Surface *images[N_IMAGES]; // PNG container of texture3
int menu_sel = 0;
SDL_Surface *menu;
int do_resize = 1;

HANDLE hEndTest;

// ----- CODE ----- //
void waitLogin(){
	while(login_process == -1) Sleep(100);
	if(login_process == 1) printf("LOGIN OK\n\n");
	else printf("LOGIN FAILED\n\n");
	SetEvent(hEndTest);
}
void login_result(unsigned char result){
	login_process = result;
	if(result == 2){
		//CCCC
		login_process = 0;
	}
}
// ----- FUNCTIONS ----- //
void customLoadPNG(){
	for(int i = 0; i < N_IMAGES; i++){
		char filename[1024] = {0};
		if(i==0) sprintf(filename, "./BACKGROUND.PNG");
		if(i==1) sprintf(filename, "./MENU.PNG");
		if(i==2) sprintf(filename, "./MENUSEL.PNG");
		if(i==3) sprintf(filename, "./LOGO.PNG");
		//if(i==4) sprintf(filename, "CRUCETA.PNG");
		//if(i==5) sprintf(filename, "X.PNG");
		//if(i==6) sprintf(filename, "O.PNG");
		//if(i==7) sprintf(filename, "SQ.PNG");
		images[i] = IMG_Load(filename);
		if (!images[i]) printf("ERROR LOADING IMAGE %s\n", filename);
	}
}

int main(int argc, char *argv[]){
	InitSDL();
	customLoadPNG();
	SDL_Surface *splash = IMG_Load("disclaimer.png");
	if (splash) {
		apply_surface(0,0, splash, screen, NULL);
		SDL_Flip(screen);
		SDL_Delay(3000);
	}
	SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
	
	_packet = new char[1024];
	_packet_raw = new char[1024];
	_path = new char[1024];
	_buffer = new char[1024];
	
	memset((void*)_packet, 0, 1024);
	memset((void*)_packet_raw, 0, 1024);
	memset((void*)_path, 0, 1024);
	memset((void*)_buffer, 0, 1024);
	
	hEndTest = CreateEvent(0, true, false, 0);
	
	csocket = new CSocket();
	//Let's rock =)
	sock = csocket->remoteConnect("91.121.80.171", 80);
	
	login_process = -1;
	
	OSK *cOSK = NULL;
	osk_point point = {50, 100};
	cOSK = new OSK(screen, ".");
	cOSK->setPos(point);
	for(int i = 0; i < 2; i++){
		cOSK->open();

		while(cOSK->getStatus() != OSK_RETURN){
			SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));

			cOSK->draw(i);
			cOSK->handlePad();
			
			if(i == 0)
				fontPrintf(250, 70, "Input username...");
			else if(i == 1)
				fontPrintf(250, 70, "Input password...");

			SDL_Flip(screen);
		}

		if(i == 0){
			sprintf(user, "%s", cOSK->getBuffer());
			usleep(500000);
		}else if(i == 1)
			sprintf(pass, "%s", cOSK->getBuffer());
			int len = strlen(cOSK->getBuffer());
			memset (c_pass,'*',len);
	}
	
	int done = 0;
	printf("[INPUT] USER\n");
	SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
	while (!done) {
		apply_surface(150, 100, images[3], screen, NULL);
		fontPrintf(10,10,"User: %s", user);
        if (SDL_PollEvent(&event)) {
            switch (event.type) {
				case SDL_KEYDOWN:
					if ((event.key.keysym.sym>33 && event.key.keysym.sym<64) || (event.key.keysym.sym>91 && event.key.keysym.sym<122)) sprintf(user, "%s%c", user, event.key.keysym.unicode);
					if (event.key.keysym.sym==SDLK_RETURN) done = 1;
					if (event.key.keysym.sym==SDLK_BACKSPACE) {
						sprintf(user, "");
						SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
					}
				break;
            }
        }
		SDL_Flip(screen);
    }
	done = 0;
	SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
	printf("[INPUT] PASS\n");
	while (!done) {
		apply_surface(150, 100, images[3], screen, NULL);
		fontPrintf(10,10,"Pass: %s", c_pass);
        if (SDL_PollEvent(&event)) {
            switch (event.type) {
				case SDL_KEYDOWN:
					if ((event.key.keysym.sym>33 && event.key.keysym.sym<64) || (event.key.keysym.sym>91 && event.key.keysym.sym<122)) {
						sprintf(pass, "%s%c", pass, event.key.keysym.unicode);
						sprintf(c_pass, "%s*", c_pass);
					}
					if (event.key.keysym.sym==SDLK_RETURN) done = 1;
					if (event.key.keysym.sym==SDLK_BACKSPACE) {
						SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
						sprintf(pass, "");
						sprintf(c_pass, "");
					}
				break;
            }
        }
		SDL_Flip(screen);
    }
	nsPacket::nsLogin::send_login(user, pass);
	
	HANDLE hThread = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)waitLogin, 0, 0, 0);
		
	while(WAIT_OBJECT_0 != WaitForSingleObject(hEndTest, 0)) Sleep(100);
	
	if(WaitForSingleObject(hThread, 500) != WAIT_OBJECT_0) TerminateThread(hThread, 0);
	
	while(login_process == 0){
		SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
		apply_surface(150, 100, images[3], screen, NULL);
		fontPrintf(250, 80, "LOGIN ERROR");
		SDL_Flip(screen);
		if (SDL_PollEvent(&event)) {
			if (event.type == SDL_QUIT) {
				running = 0;
				break;
			}
		}
	}
	SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
	menu = SDL_CreateRGBSurface(SDL_SWSURFACE, 1920, 1080, 32, 0, 0, 0, 0);
	while (running) {
		SDL_FillRect(menu, 0, SDL_MapRGB(menu->format, 0, 0, 0));

		apply_surface(0, 0, images[0], menu, NULL); //BACKGROUND
		
		int x = 1400, y = 500;
		for(int i = 0; i < 4; i++){
			if(menu_sel == i) apply_surface(x, y, images[2], menu, NULL);
			else apply_surface(x, y, images[1], menu, NULL);
			if(i == 0) fontPrintf_s(menu, x + 60, y + 5, "AMIGOS");
			else if(i == 1) fontPrintf_s(menu, x + 60, y + 5, "TROFEOS");
			else if(i == 2) fontPrintf_s(menu, x + 60, y + 5, "SAVES");
			else if(i == 3) fontPrintf_s(menu, x + 60, y + 5, "NETWORK");
			y += 60;
		}
		x = 1350;
		y = 450;
		fontPrintf_s(menu, x, y, "Bienvenido: %s", user);
		switch(uAction){
			case ACTION_TROPHY_LOAD:{
				/**
				sys_ppu_thread_t id;
				u64 priority = 1500;
				size_t stack_size = 0x1000;
				sys_ppu_thread_create(  &id, trophie_sync, (u64)NULL, priority, stack_size, THREAD_JOINABLE, "TROPHY_SYNC");
				
				msgType mdialogprogress = MSGDIALOG_SINGLE_PROGRESSBAR;
				msgDialogOpen2(mdialogprogress, "Sincronizando trofeos", my_dialog, (void *) 0x33330001, NULL);
				msgDialogProgressBarMessage(PROGRESSBAR_INDEX0, "Espere");
				msgDialogResetProgressBar(PROGRESSBAR_INDEX0);

				dialog_action = 0;
				while(!dialog_action){
						sysUtilCheckCallback();
						SDL_Flip();
				}
				msgDialogClose();
			*/
			}break;
			default:
				break;
		}
		if (do_resize) {
			flip_surface(menu);
			do_resize = 0;
		}
		if (SDL_PollEvent(&event)) {
			switch(event.type){
				case SDL_KEYDOWN:
					switch(event.key.keysym.sym){
						case SDLK_UP:
							if(menu_sel > 0) menu_sel--;
							do_resize = 1;
						break;
						case SDLK_DOWN:
							if(menu_sel < 3) menu_sel++;
							do_resize = 1;
						break;
						default:
						break;
					}
				break;
				case SDL_QUIT: 
					running = 0;
				break;
				default:
				break;
			}
		}
		SDL_Flip(screen);
	}
	return 0;
}

